//
//  TimeShowView.m
//  LeaveLight
//
//  Created by pro1 on 2019/3/14.
//  Copyright © 2019 xsl. All rights reserved.
//

#import "TimeShowView.h"
#import "TheColorLabel.h"

@interface TimeShowView ()

@property (strong,nonatomic) TheColorLabel *day;
@property (strong,nonatomic) UILabel *year;
@property (strong,nonatomic) UILabel *month;

@property (strong,nonatomic) NSString *time;

@property (assign,nonatomic) CGFloat marginLeft;


@end

@implementation TimeShowView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.time = [TimeShowView getDateWith:@""];
        
        self.marginLeft = ([self.year.text sizeWithAttributes:@{NSFontAttributeName:self.year.font}].width + 36 + 87);
        [self addSubview:self.day];
        [self addSubview:self.year];
        [self addSubview:self.month];
        
        [_year mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(30);
            make.left.equalTo(self.day.mas_right).offset(36);
            make.height.offset(30);
        }];
        
        [_month mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.year.mas_bottom).offset(15);
            make.right.equalTo(self.year.mas_right).offset(0);
            make.width.offset(18);
        }];
        
        UIBezierPath *linePath = [UIBezierPath bezierPath];
        [linePath moveToPoint:CGPointMake((self.frame.size.width - self.marginLeft)/2 + self.marginLeft + 15, 45)];
        [linePath addLineToPoint:CGPointMake((self.frame.size.width - self.marginLeft)/2 + self.marginLeft - 25, 80)];
        CAShapeLayer *lineLayer = [CAShapeLayer layer];
        lineLayer.lineWidth = 1;
        lineLayer.strokeColor = UIColorMakeWithRGBA(200, 200, 200, 1.0).CGColor;
        lineLayer.path = linePath.CGPath;
        lineLayer.fillColor = nil; // 默认为blackColor
        [self.layer addSublayer:lineLayer];
    }
    return self;
}

- (TheColorLabel *)day{
    if (!_day) {
        _day = [[TheColorLabel alloc] initWithFrame:CGRectMake((self.frame.size.width - self.marginLeft)/2, 0, 87, 60)];
        _day.font = [UIFont boldSystemFontOfSize:70];
        _day.textAlignment = NSTextAlignmentCenter;
        _day.text = [[self.time componentsSeparatedByString:@" "] objectAtIndex:1];
    }
    return _day;
}

- (UILabel *)year{
    if (!_year) {
        _year = [[UILabel alloc] init];
        _year.textColor = UIColorMakeWithHex(@"#202020");
        _year.font = [UIFont systemFontOfSize:18];
        _year.text = [[self.time componentsSeparatedByString:@" "] lastObject];
    }
    return _year;
}

- (UILabel *)month{
    if (!_month) {
        _month = [[UILabel alloc] init];
        _month.textColor = UIColorMakeWithHex(@"#202020");
        _month.font = [UIFont systemFontOfSize:18];
        _month.text = [[self.time componentsSeparatedByString:@" "] firstObject];
        _month.textAlignment = NSTextAlignmentCenter;
        _month.numberOfLines = 0;
    }
    return _month;
}

+ (NSString *)getDateWith:(NSString *)pan{
    NSDate *date = [NSDate date];
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"MM dd yyyy年";
    NSString *string = [fmt stringFromDate:date];
    string = [string stringByReplacingCharactersInRange:NSMakeRange(0, 2) withString:[TimeShowView monthEnWith:[string substringWithRange:NSMakeRange(0, 2)]]];
    return string;
}

+ (NSString *)monthEnWith:(NSString *)month{
    if ([month isEqualToString:@"01"]) {
        return @"一月";
    }
    else if ([month isEqualToString:@"02"]){
        return @"二月";
    }
    else if ([month isEqualToString:@"03"]){
        return @"三月";
    }
    else if ([month isEqualToString:@"04"]){
        return @"四月";
    }
    else if ([month isEqualToString:@"05"]){
        return @"五月";
    }
    else if ([month isEqualToString:@"06"]){
        return @"六月";
    }
    else if ([month isEqualToString:@"07"]){
        return @"七月";
    }
    else if ([month isEqualToString:@"08"]){
        return @"八月";
    }
    else if ([month isEqualToString:@"09"]){
        return @"九月";
    }
    else if ([month isEqualToString:@"10"]){
        return @"十月";
    }
    else if ([month isEqualToString:@"11"]){
        return @"十一月";
    }
    else if ([month isEqualToString:@"12"]){
        return @"十二月";
    }
    else{
        return @"";
    }
}


@end
